package com.store.controller;

import com.store.configure.AliyunOCRConfig;
import com.store.configure.Result;
import com.store.utils.AliyunOCRUtil;
import com.store.utils.IDGenerator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * 文件上传
 * WEN.C
 */
@RestController
@RequestMapping("/fileUtils")
public class FileUtils {

    @Value("${upload.file.path}")
    private String imagePath;


    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public Object uploadFiles(@RequestParam("file") List<MultipartFile> files, HttpServletRequest request) throws Exception {


        // 无法获取全局变量，当前用户ID
        LocalDate localDate = LocalDate.now();
        // 保存路径
        String UPLOAD_FILES_PATH = localDate.getYear() + "/" + localDate.getMonthValue() + "/" + localDate.getDayOfMonth() + "/";
        if (Objects.isNull(files) || files.isEmpty()) {
            Result result = new Result();
            result.setCode("0");
            result.setMessage("文件为空，请重新上传");
            return result;
        }
        for (MultipartFile file : files) {

            // String fileName = IDGenerator.getUUID("FILE") +file.getOriginalFilename();
            String fix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            if (!".jpg".equalsIgnoreCase(fix)
                    && !".jpeg".equalsIgnoreCase(fix)
                    && !".png".equalsIgnoreCase(fix)
                    && !".bmp".equalsIgnoreCase(fix)) {
                throw new Exception("非法的文件类型");
            }
            String fileName = IDGenerator.getUUID("FILE") + fix;
            if (file.isEmpty()) {
                return "false";
            } else {
                File dest = new File(imagePath + UPLOAD_FILES_PATH + fileName);
                UPLOAD_FILES_PATH += fileName + ",";
                if (!dest.getParentFile().exists()) { //判断文件父目录是否存在
                    dest.getParentFile().mkdirs();
                }
                try {
                    file.transferTo(dest);
                } catch (Exception e) {
                    e.printStackTrace();
                    Result result = new Result();
                    result.setCode("0");
                    result.setMessage("文件上传");
                    return result;
                }
            }

        }
        if (UPLOAD_FILES_PATH.length() > 0) {
            UPLOAD_FILES_PATH = UPLOAD_FILES_PATH.substring(0, UPLOAD_FILES_PATH.length() - 1);
        }

        Result result = new Result();
        result.setCode("0");
        result.setMessage("文件上传");
        result.setReturnObj(UPLOAD_FILES_PATH);
        return result;
    }

    @RequestMapping(value = "/uploadPicUrl", method = RequestMethod.POST)
    public Result<String> uploadPicUrl(@RequestParam("file") List<MultipartFile> files, HttpServletRequest request) throws Exception {

        Result<String> result = new Result<>();
        if (Objects.isNull(files) || files.isEmpty()) {
            result.setCode("0");
            result.setMessage("文件为空，请重新上传");
            return result;
        }

        String returnStr = AliyunOCRUtil.idCardAnalysis(files.get(0).getInputStream(), new AliyunOCRConfig());

        result.setReturnObj(returnStr);
        result.setMessage("success");

        return result;
    }
}
